package com.ruoyi.cs.event.followRecord.service;

import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.base.customer.domain.CustomerInfo;
import com.ruoyi.cs.base.customer.mapper.CustomerInfoMapper;
import com.ruoyi.cs.base.supplier.domain.SupplierInfo;
import com.ruoyi.cs.base.supplier.mapper.SupplierInfoMapper;
import com.ruoyi.cs.event.followRecord.domain.FollowRecord;
import com.ruoyi.cs.event.followRecord.domain.FollowRecordCond;
import com.ruoyi.cs.event.followRecord.mapper.FollowRecordMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 跟进记录业务逻辑
 */
@Service
public class FollowRecordServiceImpl implements IFollowRecordService{
    @Autowired
    private FollowRecordMapper followRecordMapper;
    @Autowired
    private CustomerInfoMapper customerInfoMapper;
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;
    @Autowired
    private IdUtil idUtil;
    /**
     * 查找跟进记录
     * @param cond
     * @return
     */
    @Override
    public List<FollowRecord> selectFollowRecordList(FollowRecordCond cond) {
        List<FollowRecord> followRecordList = followRecordMapper.selectFollowRecordList(cond);
        for (FollowRecord s : followRecordList) {
            if (s.getAssociationType().equals(ERPConstant.CS_CUSTOMER)) {
                // 关联类型为客户
                CustomerInfo customerInfo = customerInfoMapper.selectCustomerInfoById(s.getAssociationId());
                s.setAssociationName(customerInfo.getCustomerInfoName());
            } else {
                // 关联类型为供应商
                SupplierInfo supplierInfo = supplierInfoMapper.selectSupplierInfoById(s.getAssociationId());
                s.setAssociationName(supplierInfo.getSupplierInfoName());
            }
        }
        return followRecordList;
    }

    /**
     * 新增跟进记录
     * @param followRecord
     * @return
     */
    @Override
    public int insertFollowRecord(FollowRecord followRecord) {
        if (StringUtils.isNotNull(followRecordMapper.selectFollowRecordById(followRecord.getFollowId()))) {
            //存在
            return 0;
        }
        //不存在
        String followId = idUtil.createFollowId();
        followRecord.setFollowId(followId);
        return followRecordMapper.insertFollowRecord(followRecord);
    }

    /**
     * 更新跟进记录
     * @param followRecord
     * @return
     */
    @Override
    public int updateFollowRecord(FollowRecord followRecord) {
        if (!StringUtils.isNotNull(followRecordMapper.selectFollowRecordById(followRecord.getFollowId()))) {
            //不存在
            return 0;
        }
        //存在销售订单出库单据
        return followRecordMapper.updateFollowRecord(followRecord);
    }

    /**
     * 删除跟进记录
     * 仅修改跟进记录删除标志
     * @param followRecord
     * @return
     */
    @Override
    public int updateFollowRecordDeleteFlag(FollowRecord followRecord) {
        return followRecordMapper.updateFollowRecordDeleteFlag(followRecord);
    }
}
